package servlet;

import util.*;

import java.io.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Update extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取客户端请求的参数，并进行判断。

        request.setCharacterEncoding("utf-8");
        String id=request.getParameter("id").trim();//取得用户名参数
        String name=request.getParameter("name").trim();
        String sex=request.getParameter("sex").trim();
        String birthday=request.getParameter("birth").trim();
        String education=request.getParameter("degree").trim();
        String native_place=request.getParameter("origin").trim();
        String place=request.getParameter("address").trim();
        String phone=request.getParameter("tel").trim();
        double work_age=Double.parseDouble(request.getParameter("wyear").trim());
        double base_pay=Double.parseDouble(request.getParameter("wage").trim());
        try{
            DButil dButil = new DButil();
            Connection con = dButil.getConnection();

            String sql = "update staff set name=?,sex=?,birth=?,degree=?,origin=?,address=?,tel=?,wyear=?,wage=? where  id='" + id + "'";

            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, sex);
            ps.setString(3, birthday);
            ps.setString(4, education);
            ps.setString(5, native_place);
            ps.setString(6, place);
            ps.setString(7, phone);
            ps.setDouble(8, work_age);
            ps.setDouble(9, base_pay);
            if (ps.executeUpdate() != 0){
                RequestDispatcher dispatcher =
                        request.getRequestDispatcher("index.jsp");//转发
                dispatcher.forward(request, response);
            }
            else
            {
                RequestDispatcher dispatcher=
                        request.getRequestDispatcher("index.jsp");//转发
                dispatcher.forward(request,response);

            }
        }catch(Exception e){
            e.printStackTrace();

        }
        RequestDispatcher dispatcher=
                request.getRequestDispatcher("index.jsp");//转发
        dispatcher.forward(request,response);

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request,response);
    }
}
